		TITLE	QUIK-SUBS Copyright (c) SLR Systems, Inc 1994

		INCLUDE MACROS

if	fg_dospack OR fg_winpack

		INCLUDE SLR32

		PUBLIC	PUTSTUFF_SMALL_QUIK32,PUTSTUFF_QUIK32,FLUSHSTUFF_QUIK32,FIX_BUFFERS_QUIK32,PUT_0,PUT_1,FLUSH_PUTSTUFF


		.DATA

		EXTERNDEF	HUF_MINI_BUFFER:BYTE

		EXTERNDEF	SLR_MASK:DWORD,SLR_WORD:DWORD,SLR_CPOS:DWORD,SLR_VARS:DWORD,SLR_TMP_PTR:DWORD


		.CODE	PASS2_TEXT

		EXTERNDEF	?_KEEP_QUIK_COMPRESSED:PROC,MOVE_EAX_TO_COMPRESS_OUTPUT:PROC,DOT:PROC


PUTSTUFF_SMALL_QUIK32	PROC	NEAR
		;
		;AL IS NORMAL BYTE TO OUTPUT
		;
		PUSHM	EDI,EBX

		MOV	EBX,SLR_MASK
		MOV	ECX,SLR_WORD

		OR	ECX,EBX
		MOV	EDI,SLR_TMP_PTR

		SHR	EBX,1
		MOV	SLR_WORD,ECX

		MOV	SLR_MASK,EBX
		JZ	L101$
L1019$:
		MOV	[EDI],AL
		INC	EDI

		MOV	SLR_TMP_PTR,EDI
		POP	EBX

		POP	EDI

		RET

L101$:
		PUSH	EDX
		CALL	FLUSH_PUTSTUFF

		POP	EDX
		JMP	L1019$

PUTSTUFF_SMALL_QUIK32	ENDP


FIX_BUFFERS_QUIK32	PROC	NEAR
		;
		;TELL EXPANDER TO FIX UP POINTERS
		;ALSO TELL COMPRESSER WE JUST DID ANOTHER 16K
		;
		MOV	EAX,81H-THRESHOLD

FIX_BUFFERS_QUIK32	ENDP


PUTSTUFF_QUIK32 PROC	NEAR
		;
		;EAX IS CHAR, EBX IS DISTANCE
		;
		;NOW STORE CHAR
		;
		MOV	EDI,SLR_TMP_PTR
		MOV	EDX,SLR_MASK
		;
		;>255, OUTPUT A ZERO BIT
		;
		SHR	EDX,1		;0
		JZ	L20$

		MOV	SLR_MASK,EDX
L209$:
		;
		;FIRST WE OUTPUT COPY COUNT
		;
		;
		;FIRST TRY 2-3
		;
		PUSH	EAX

		CMP	AL,3-THRESHOLD
		JA	L1$
L19$:
		CALL	PUT_0			;0X		2-3

		JMP	L49$

L20$:
		MOV	SLR_MASK,EDX
		CALL	FLUSH_PUTSTUFF

		JMP	L209$

L1$:
		SUB	AL,4-THRESHOLD
		CALL	PUT_1			;1

		CMP	AL,2			;TRY 4-5
		JB	L19$

		SUB	AL,2
		CALL	PUT_1			;11

		CMP	AL,2			;TRY 6-7
		JB	L19$

		SUB	AL,2
		CALL	PUT_1			;111

		CMP	AL,4			;TRY 8-11
		JAE	L2$

		CALL	PUT_0			;1110XX		8-11

		CALL	PUT_AL_TWO_BITS

		JMP	L5$

L2$:
		SUB	AL,4
		CALL	PUT_1			;1111

		CMP	AL,8			;TRY 12-19
		JAE	L3$

		CALL	PUT_0			;11110XXX	12-19

		CALL	PUT_AL_3_BITS

		JMP	L5$

L3$:
		POP	EAX
		CALL	PUT_1			;11111

		PUSH	EAX
		ADD	AL,THRESHOLD

		MOV	[EDI],AL
		INC	EDI

		JMP	L5$

L49$:
		ROR	AL,1
		CALL	PUT_0_OR_1
L5$:
		;
		;NOW SEND POSITION PLEASE
		;
		POP	EAX

		CMP	AL,2-THRESHOLD		;TWO JUST GETS 8-BIT OFFSET
		JZ	L8$

		CMP	AL,80H-THRESHOLD
		JA	L9$			;ADJUST, QUIT, ETC GET NOTHING...

		PUSH	EBX
		MOV	AL,BH

		CMP	AL,4
		JAE	L6$

		CALL	PUT_0			;00		0

		CMP	AL,0
		JA	L51$
L50$:
		CALL	PUT_0

		POP	EBX
		JMP	L81$

L51$:
		CALL	PUT_1			;010		1

		CMP	AL,1
		JZ	L50$

		CALL	PUT_1			;0110		2

		ROR	AL,1			;0111		3
		CALL	PUT_0_OR_1

		POP	EBX
		JMP	L81$

L6$:
		CALL	PUT_1

		CMP	AL,16
		JAE	L7$

		CALL	PUT_0

		CMP	AL,8
		JAE	L65$

		CALL	PUT_0

		SUB	AL,4			;100XX		4-7
		CALL	PUT_AL_TWO_BITS

		POP	EBX
		JMP	L81$

L65$:
		CALL	PUT_1			;101XXX		8-F

		SUB	AL,8
		CALL	PUT_AL_3_BITS

		POP	EBX
		JMP	L81$

L7$:
		CALL	PUT_1

		CMP	AL,20H
		JAE	L71$
L70$:
		CALL	PUT_0			;110XXXX	10-1F

		CALL	PUT_AL_4_BITS

		POP	EBX
		JMP	L81$

L71$:
		CALL	PUT_1			;111

		CMP	AL,30H
		JB	L70$			;1110XXXX	20-2F

		CALL	PUT_1			;1111

		CMP	AL,40H
		JB	L70$			;11110XXXX	30-3F

		CALL	PUT_1			;11111XXXXXX	40-7F

		ROR	AL,6
		CALL	PUT_TWO_BITS

		CALL	PUT_FOUR_BITS

		POP	EBX
L8$:
L81$:
		MOV	[EDI],BL
		INC	EDI
L9$:
		MOV	SLR_TMP_PTR,EDI

		RET

PUT_AL_3_BITS:
		ROR	AL,3

		ADD	AL,AL
		CALL	PUT_0_OR_1

		JMP	PUT_TWO_BITS

PUT_AL_4_BITS:
		ROR	AL,4
PUT_FOUR_BITS:
		CALL	PUT_TWO_BITS

		JMP	PUT_TWO_BITS

PUT_AL_TWO_BITS:
		ROR	AL,2
PUT_TWO_BITS:
		ADD	AL,AL
		CALL	PUT_0_OR_1

		ADD	AL,AL
PUT_0_OR_1:
		JC	PUT_1
		;
		;PUT A ZERO
		;
PUT_0::
		SHR	SLR_MASK,1
		JZ	FLUSH_PUTSTUFF

		RET

PUT_1::
		MOV	ECX,SLR_WORD
		MOV	EDX,SLR_MASK

		OR	ECX,EDX

		SHR	EDX,1
		MOV	SLR_WORD,ECX

		MOV	SLR_MASK,EDX
		JZ	FLUSH_PUTSTUFF

		RET

PUTSTUFF_QUIK32 ENDP


FLUSH_PUTSTUFF	PROC	NEAR
		;
		;NEED NEW MASK
		;
		PUSHM	EAX
		MOV	ECX,SLR_CPOS

		MOV	EAX,SLR_WORD
		CMP	EDI,OFF HUF_MINI_BUFFER+HMB_SIZE-28

		MOV	SLR_MASK,8000H

		MOV	[ECX],AX
		JA	L51$
L52$:
		MOV	SLR_CPOS,EDI
		XOR	EAX,EAX

		MOV	[EDI],AL
		MOV	SLR_WORD,EAX

		MOV	[EDI+1],AL
		ADD	EDI,2

		POP	EAX

		RET

L51$:
		CALL	FLUSH_MINI
		JMP	L52$

FLUSH_PUTSTUFF	ENDP


FLUSHSTUFF_QUIK32	PROC	NEAR
		;
		;ALL DONE, SIGNAL END OF IT ALL
		;
		MOV	EAX,82H-THRESHOLD
		CALL	PUTSTUFF_QUIK32
		;
		;NOW FLUSH LZ_WORD BY WRITING 15 ZEROS
		;
		CALL	FLUSH_PUTSTUFF

		SUB	EDI,2
		CALL	FLUSH_MINI

		JMP	?_KEEP_QUIK_COMPRESSED

FLUSH_MINI::
		;
		;FLUSH STUFF FROM HUF_MINI_BUFFER
		;
		PUSHM	EDX,ECX

		MOV	ECX,EDI
		MOV	EAX,OFF HUF_MINI_BUFFER

		SUB	ECX,EAX
		JZ	L9$

		CALL	MOVE_EAX_TO_COMPRESS_OUTPUT

		MOV	EDI,OFF HUF_MINI_BUFFER
L9$:
		POPM	ECX,EDX

		RET

FLUSHSTUFF_QUIK32	ENDP

endif

		END

